#include "RingQueue.hpp"
#include "Task.hpp"

#include<iostream>
#include<unistd.h>
#include<pthread.h>
using namespace std;

string op_arr("+-*/%"); //运算符

//消费者
void* ConsumerRoutine(void* args)
{
    RingQueue<Task>* CRQ = static_cast<RingQueue<Task>*>(args);
    while(true)
    {
        Task t;
        CRQ->pop(t);

        t();
        cout << "ConsumerRoutine: ";
        t.showRet();
        cout << endl;
    }
}

//生产者
void* ProducerRoutine(void* args)
{
    RingQueue<Task>* PRQ = static_cast<RingQueue<Task>*>(args);
    while(true)
    {
        int left = rand() % 10;
        int right = rand() % 10;
        char op = op_arr[rand() % 5];

        Task t(left, right, op);
        PRQ->push(t);

        cout << "ProducerRoutine: ";
        t.showExp();
        sleep(1);
    }
}

int main()
{
    srand(time(nullptr));
    RingQueue<Task>* RQ = new RingQueue<Task>();//循环队列
    pthread_t c, p;

    //线程创建
    pthread_create(&c, nullptr, ConsumerRoutine, RQ);
    pthread_create(&p, nullptr, ProducerRoutine, RQ);

    //释放回收线程
    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    delete RQ;
    return 0;
}